﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WeiboPlus.Common;
using WeiboPlus.Models;
using WeiboPlus.Shell.ViewModel;

namespace WeiboPlus.Shell.Views
{
    public partial class AccountManagmentView : PhoneApplicationPage
    {
        bool hasInit = false;
        SettingViewModel _vm;
        public AccountManagmentView()
        {
            InitializeComponent();

            DataContext = _vm = new SettingViewModel();

            this.Loaded += (o, e) =>
            {
                if (hasInit)
                    return;
                hasInit = true;

                var back = new ApplicationBarIconButton(new Uri("/Images/AppBarIcons/appbar.back.rest.png", UriKind.Relative)) { Text = AppResx.GoBackText };
                back.Click += new EventHandler(back_Click);
                ApplicationBar.Buttons.Add(back);

                var item1 = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Images/AppBarIcons/refresh.png", UriKind.Relative),
                    Text = AppResx.RefreshText,
                };

                item1.Click += OnRefreshClick;
                this.ApplicationBar.Buttons.Add(item1);

                //var item2 = new ApplicationBarIconButton
                //{
                //    IconUri = new Uri("/Images/AppBarIcons/delete.png", UriKind.Relative),
                //    Text = AppResx.DeleteAccountText,
                //};

                //item2.Click += OnDeleteAccountClick;

                //this.ApplicationBar.Buttons.Add(item2);
            };
        }

        private void OnRefreshClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MainView.xaml", UriKind.Relative));
        }

        private void OnDeleteAccountClick(object sender, EventArgs e)
        {
            if (!_vm.BindedAccounts.Any(account => account.IsDelete))
            {
                MessageBox.Show(AppResx.WarnNoAccountSelectedText);
                return;
            }

            _vm.DeleteAccount();
        }

    

        private void accountList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                foreach (var item in e.AddedItems)
                {
                    var account = item as SNSAccount;
                    if (account != null)
                    {
                        account.IsDelete = true;
                    }
                }
            }

            if (e.RemovedItems.Count > 0)
            {
                foreach (var item in e.RemovedItems)
                {
                    var account = item as SNSAccount;
                    if (account != null)
                    {
                        account.IsDelete = false;
                    }
                }
            }
        }

        private void ViewUserHome(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var fe = e.OriginalSource as FrameworkElement;
            var acc = fe.DataContext as SNSAccount;
            var user = acc.UserLite;
            var key = user.AccountName + "=" + user.AccountID;

            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }

            PhoneApplicationService.Current.State.Add(key, user);
            NavigationService.Navigate(new Uri("/Views/UserHomeView.xaml?" + key, UriKind.Relative));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (PhoneApplicationController.Current.AppSetting.EnableOrientationChange)
            {
                SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            }
            else
            {
                var orientation = (SupportedPageOrientation)PhoneApplicationController.Current.AppSetting.CurrentSupportedOrientation;
                this.SupportedOrientations = orientation;
                this.Orientation = (PageOrientation)PhoneApplicationController.Current.AppSetting.CurrentOrientation;
            }
        }

        void back_Click(object sender, EventArgs e)
        {
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
            //throw new NotImplementedException();
        }

        private void MenuItemSave_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var acc = fe.DataContext as SNSAccount;

            if (acc != null)
            {
                _vm.DeleteAccount(acc);
            }
        }

    }
}